package com.example.hasee.zhcd.Activities.ShangWu;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.hasee.zhcd.Activities.ChuanWu.OBInputActivity;
import com.example.hasee.zhcd.Base.BaseActivity;

import com.example.hasee.zhcd.Bean.EventMsg;
import com.example.hasee.zhcd.R;
import com.example.hasee.zhcd.Util.DateTimePickDialogUtil;
import com.example.hasee.zhcd.Util.GlobalParameter;
import com.example.hasee.zhcd.Util.ProgressDialogUtils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;


/**
 * Created by hasee on 2018/6/2.
 */

public class FundActivity extends BaseActivity {
    private TextView title;
    private ImageView back;
    private ImageView home;
    private EditText fund;
    private EditText date;
    private EditText amount;
    private EditText beizhu;
    private Button btn_save;
    private String strSYN;
    private String strShip;
    private String strLY;
    private String strBYJ;
    private String strSQSJ;
    private String strBZ;
    private String strAccount;
    private ArrayList<HashMap<String, String>> fundlist = new ArrayList<HashMap<String, String>>();
    private HashMap<String, String> loginmap = (HashMap<String, String>) GlobalParameter.get("loginmap");

    @Override
    public int contentViewId() {
        return R.layout.activity_fund;
    }

    @Override
    public void set() {
        title.setText("备用金录入");
        home.setVisibility(View.INVISIBLE);
        back.setOnClickListener(this);
        fund.setOnClickListener(this);
        date.setOnClickListener(this);
        btn_save.setOnClickListener(this);
        getfund();
    }

    @Override
    public void init() {
        strAccount = loginmap.get("account");
        strSYN = getIntent().getExtras().getString("strSYN");
        strShip = getIntent().getExtras().getString("strShip");
        back = (ImageView) findViewById(R.id.back);
        home = (ImageView) findViewById(R.id.home);
        title = (TextView) findViewById(R.id.centertitle);
        fund = (EditText) findViewById(R.id.fund);
        date = (EditText) findViewById(R.id.date);
        amount = (EditText) findViewById(R.id.amount);
        beizhu = (EditText) findViewById(R.id.beizhu);
        btn_save = (Button) findViewById(R.id.btn_save);
    }

    @Override
    public void getEventData(EventMsg msg) {
        switch (msg.getAction()) {
            case "ZHSW_zgcb_byj":
                ProgressDialogUtils.closeProgressDialog();
//                Log.e("ZHSW_zgcb_byj", msg.getMsg());
                //ZHSW_zgcb_byj: {"data": [{"fcode":"48", "fname":"农行POS"},{"fcode":"42", "fname":"交行账户"},{"fcode":"63", "fname":"农行卡"},{"fcode":"7", "fname":"现金收款"}],"rspcode": 0,"rspmessage":""}
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("0".equals(rspcode)) {
                        JSONArray data = json.getJSONArray("data");
                        for (int i = 0; i < data.length(); i++) {
                            HashMap<String, String> map = new HashMap<String, String>();
                            map.put("fname", data.getJSONObject(i).getString("fname"));
                            map.put("fcode", data.getJSONObject(i).getString("fcode"));
                            fundlist.add(map);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case "ZHSW_zgcb_saveBYJ":
                ProgressDialogUtils.closeProgressDialog();
//                Log.e("ZHSW_zgcb_saveBYJ", msg.getMsg());
                //ZHSW_zgcb_saveBYJ: {"data":"","rspcode": 1,"rspmessage":"保存成功！"}
                try {
                    JSONObject json = new JSONObject(msg.getMsg());
                    String rspcode = json.getString("rspcode");
                    String rspmessage = json.getString("rspmessage");
                    if ("1".equals(rspcode)) {
                        FinishDialog(rspmessage);
                    } else {
                        Toast.makeText(FundActivity.this, rspmessage, Toast.LENGTH_LONG).show();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    private void getfund() {
        ProgressDialogUtils.createProgressDialog(FundActivity.this, "正在获取退款类型...");
        HashMap<String, String> map = new HashMap<String, String>();
        httpUtils.get("ZHSW_zgcb_byj", map, "ZHSW_zgcb_byj");
    }

    private void savefund() {
        ProgressDialogUtils.createProgressDialog(FundActivity.this, "正在提交退款...");
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("strSYN", strSYN);
        map.put("strShip", strShip);
        map.put("strLY", strLY);
        map.put("strBYJ", strBYJ);
        map.put("strSQSJ", strSQSJ);
        map.put("strBZ", strBZ);
        map.put("strAccount", strAccount);
        httpUtils.get("ZHSW_zgcb_saveBYJ", map, "ZHSW_zgcb_saveBYJ");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.home:
                break;
            case R.id.fund:
                singleDialog();
                break;
            case R.id.date:
                String t1 = "";
                if (date.getText().toString().equals("")) {
                    t1 = getTime();
                } else {
                    t1 = date.getText().toString();
                }
                DateTimePickDialogUtil dialog1 = new DateTimePickDialogUtil(this,
                        t1);
                dialog1.dateTimePicKDialog(date);
                break;
            case R.id.btn_save:
                strBYJ = amount.getText().toString().trim();
                strSQSJ = date.getText().toString().trim();
                strBZ = beizhu.getText().toString().trim();
                View errorview = inputCheck();
                if (errorview != null) {
                    errorview.setFocusable(true);
                    errorview.requestFocus();
                    errorview.setFocusableInTouchMode(true);
                    return;
                } else {
                    savefund();
                }
                break;
        }
    }

    private View inputCheck() {
        if (strLY.length() == 0) {
            Toast.makeText(FundActivity.this, "请选择来源！", Toast.LENGTH_LONG).show();
            return fund;
        } else if (strSQSJ.length() == 0) {
            Toast.makeText(FundActivity.this, "请选择时间", Toast.LENGTH_LONG).show();
            return date;
        } else if (strBYJ.length() == 0) {
            Toast.makeText(FundActivity.this, "请输入金额！", Toast.LENGTH_LONG).show();
            return amount;
        }
        return null;
    }

    public String getTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
        return df.format(new Date());// new Date()为获取当前系统时间
    }

    public void singleDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();
        final View layout = inflater.inflate(R.layout.list_dialog, null);
        builder.setView(layout);
        ListView shiplist = (ListView) layout.findViewById(R.id.dialog_list);
        final SingleAdapter singleadapter = new SingleAdapter();
        shiplist.setAdapter(singleadapter);
        final AlertDialog dialog = builder.create();
        Button ship_sure = (Button) layout.findViewById(R.id.ship_sure);
        ship_sure.setVisibility(View.GONE);
        shiplist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                fund.setText(fundlist.get(position).get("fname"));
                strLY = fundlist.get(position).get("fcode");
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    private void FinishDialog(String message) {
        AlertDialog.Builder builder = new AlertDialog.Builder(FundActivity.this);
        builder.setMessage(message);
        builder.setTitle("提示");
        builder.setNegativeButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface arg0, int arg1) {
                // TODO Auto-generated method stub
                arg0.dismiss();
                FundActivity.this.finish();
            }
        });
        builder.create().setCanceledOnTouchOutside(false);
        builder.setCancelable(false);
        builder.show();
    }

    class SingleAdapter extends BaseAdapter {
        private LayoutInflater itemInflater = FundActivity.this.getLayoutInflater();
        private Holder holder;

        @Override
        public int getCount() {
            return fundlist.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                holder = new Holder();
                convertView = itemInflater.inflate(R.layout.role_list_item, null);
                holder.fundkind = (TextView) convertView.findViewById(R.id.ship_text);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.fundkind.setText((String) fundlist.get(position).get("fname"));

            return convertView;
        }

        private class Holder {
            private TextView fundkind;
        }
    }
}
